package com.ruijie.acl.service;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.util.Assert;

import com.ruijie.acl.AclPlanParameters;
import com.ruijie.acl.utils.AclUtil;
import com.ruijie.acl.web.config.Constants;
import com.ruijie.batch.plan.BatchPlan;
import com.ruijie.batch.plan.PlanDefinitionRequest;
import com.ruijie.batch.plan.PlanDefinitionService;
import com.ruijie.batch.task.Task;
import com.ruijie.batch.task.TaskRegistry;
import com.ruijie.emp.device.Device;

/**
 * <p>
 * Title: ACL计划参数服务实现类
 * </p>
 * <p>
 * Description: Function Description
 * </p>
 * <p>
 * Copyright: Copyright (c) 2009
 * </p>
 * <p>
 * Company: Ruijie Co., Ltd.
 * </p>
 * <p>
 * Create Time: 2009-09-27
 * </p>
 * <p>
 * Update Time:
 * </p>
 * <p>
 * Updater:
 * </p>
 * <p>
 * Update Comments:
 * </p> *
 * 
 * @author ASAI
 */
public class AclPlanParametersServiceImpl extends AclHibernateEntityExtendDao<AclPlanParameters> implements
		AclPlanParametersService {

	private PlanDefinitionService planDefinitionService;
	private TaskRegistry taskRegistry;

	/**
	 * 根据计划和设备取得参数
	 * 
	 * @param batchPlan
	 *            计划
	 * @param device
	 *            设备
	 * @return 参数
	 */
	@SuppressWarnings("unchecked")
	public AclPlanParameters findByDevicePlan(BatchPlan batchPlan, Device device) {
		Assert.notNull(batchPlan);
		Assert.notNull(device);
		Criteria criteria = createCriteria(AclPlanParameters.class);
		criteria.add(Restrictions.eq("batchPlan", batchPlan));
		criteria.add(Restrictions.eq("device", device));
		List<AclPlanParameters> planParamters = (List<AclPlanParameters>) criteria.list();
		if (planParamters.size() > 0) {
			return planParamters.get(0);
		} else {
			return null;
		}
	}

	/**
	 * 保存
	 * 
	 * @param entity
	 *            参数
	 */
	public void saveAclPlanParameters(AclPlanParameters entity) {
		entity.setBatchPlan(saveBatchPlan("System-AclDeploy-" + AclUtil.createUuid()));

		this.save(entity);
	}

	/**
	 * 计划名称是否重复
	 * 
	 * @param planName
	 * @param id
	 * @return
	 */
	public boolean existBatchPlanWithName(String planName, Long id) {
		Assert.hasText(planName);
		List<BatchPlan> result = findBatchPlanByName(planName, Constants.ACL_DEPLOY_ACTION_TASK_CODE_PREX, id, true);
		if (!result.isEmpty()) {
			return true;
		}
		return false;
	}
	
	@SuppressWarnings("unchecked")
	protected List<BatchPlan> findBatchPlanByName(String planName, String taskCode, Long id, boolean enableAnyLike) {
		Assert.hasText(planName);
		Assert.hasText(taskCode);

		Criteria criteria = createCriteria(BatchPlan.class);
		criteria.add(Restrictions.eq("name", planName));
		if (id != null){
			criteria.add(Restrictions.not(Restrictions.eq("id", id)));
		}
		
		Criteria taskCriteria = criteria.createCriteria("task", "ts");
		
		if (enableAnyLike) {
			taskCriteria.add(Restrictions.like("code", taskCode, MatchMode.ANYWHERE));
		} else {
			taskCriteria.add(Restrictions.eq("code", taskCode));
		}

		return criteria.list();
	}

	@SuppressWarnings("unchecked")
	protected List<BatchPlan> findBatchPlanByName(String planName, String taskCode, boolean enableAnyLike) {
		return findBatchPlanByName(planName,taskCode,null,enableAnyLike);
	}

	/**
	 * 保存计划
	 * 
	 * @param planName
	 *            计划名
	 * @return 保存的计划
	 */
	public BatchPlan saveBatchPlan(String planName) {
		PlanDefinitionRequest request = new PlanDefinitionRequest();
		request.setPlanType(PlanDefinitionRequest.PLAN_TYPE__ADHOC);
		request.applyIgnoreException();
		request.setTask(getTask());
		request.setPlanName(planName);
		return planDefinitionService.saveOrUpdateBatchPlan(request);
	}

	/**
	 * 取得任务
	 * 
	 * @return 任务
	 */
	public Task getTask() {
		return taskRegistry.getTask(Constants.ACL_DEPLOY_ACTION_TASK_CODE_MULTIPLE);
	}

	public PlanDefinitionService getPlanDefinitionService() {
		return planDefinitionService;
	}

	public void setPlanDefinitionService(PlanDefinitionService planDefinitionService) {
		this.planDefinitionService = planDefinitionService;
	}

	public TaskRegistry getTaskRegistry() {
		return taskRegistry;
	}

	public void setTaskRegistry(TaskRegistry taskRegistry) {
		this.taskRegistry = taskRegistry;
	}

	/**
	 * 根据设备删除计划参数
	 * 
	 * @param device
	 *            设备
	 */
	public void removeByDevice(Device device) {
		String hql = "delete from AclPlanParameters where device=?";
		this.createQuery(hql, device).executeUpdate();
	}
	
	/**
	 * 根据ID列表删除计划参数
	 * @param ids ID列表
	 */
	public void removeByIds(String ids){
		Assert.hasText(ids);
		String hql = "delete from AclPlanParameters where id in (" +ids+")";
		this.createQuery(hql).executeUpdate();
	}
}
